using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

using TWrite = ProjectNoClue.Part;

namespace ProjectNoClue.Pipeline
{
	[ContentTypeWriter]
	public class PartWriter : ContentTypeWriter<TWrite>
	{
		protected override void Write(ContentWriter output, TWrite value)
		{
			output.Write(value.Position);
			output.Write(value.Rotation);
			output.Write(value.Shapes.Length);
			for (int ii = 0; ii < value.Shapes.Length; ++ii)
			{
				WriteShape(output, value.Shapes[ii]);
			}
		}

		void WriteShape(ContentWriter output, Part.Shape value)
		{
			output.Write(value.ShapeColor.PackedValue);
			output.Write(value.Vertices.Length);
			for (int ii = 0; ii < value.Vertices.Length; ++ii)
			{
				output.Write(value.Vertices[ii]);
			}
			output.Write(value.HiddenEdges.Length);
			for (int ii = 0; ii < value.HiddenEdges.Length; ++ii)
			{
				output.Write(value.HiddenEdges[ii]);
			}

			output.Write(value.Edges.Length);
			for (int ii = 0; ii < value.Edges.Length; ++ii)
			{
				WriteEdge(output, ref value.Edges[ii]);
			}
		}

		void WriteEdge(ContentWriter output, ref Part.Shape.Edge value)
		{
			output.Write(value.LenghtSinCos);
			output.Write(value.Point);
		}

		public override string GetRuntimeReader(TargetPlatform targetPlatform)
		{
			return "ProjectNoClue.Pipeline.Readers.PartReader, NoClueTypes";
		}
	}
}
